package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YyMessage;
import com.ruoyi.system.service.IYyMessageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息管理Controller
 * 
 * @author ruoyi
 * @date 2024-04-23
 */
@Controller
@RequestMapping("/system/message")
public class YyMessageController extends BaseController
{
    private String prefix = "system/message";

    @Autowired
    private IYyMessageService yyMessageService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysRoleService iSysRoleService;

    @RequiresPermissions("system:message:view")
    @GetMapping()
    public String message()
    {
        return prefix + "/message";
    }

    /**
     * 查询消息管理列表
     */
    @RequiresPermissions("system:message:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyMessage yyMessage)
    {
        startPage();
        List<YyMessage> list = yyMessageService.selectYyMessageList(yyMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息管理列表
     */
    @RequiresPermissions("system:message:export")
    @Log(title = "消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyMessage yyMessage)
    {
        List<YyMessage> list = yyMessageService.selectYyMessageList(yyMessage);
        ExcelUtil<YyMessage> util = new ExcelUtil<YyMessage>(YyMessage.class);
        return util.exportExcel(list, "消息管理数据");
    }

    /**
     * 新增消息管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //根据userId 获取 角色ID 判断 显示（管理用户要发的消息：）或 显示（家长恢复的消息：）
        SysRole sysRole = iSysRoleService.getRoleIdByUserId(getUserId());
        if (sysRole.getRoleId() == 2){
            mmap.put("roleId", "2");
        }else if (sysRole.getRoleId() == 4){
            mmap.put("roleId", "4");
        }
        mmap.put("jzUser", iSysUserService.getUserAll());
        return prefix + "/add";
    }

    /**
     * 新增保存消息管理
     */
    @RequiresPermissions("system:message:add")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyMessage yyMessage)
    {
        yyMessage.setAdminUserId(getUserId());
        return toAjax(yyMessageService.insertYyMessage(yyMessage));
    }

    /**
     * 修改消息管理
     */
    @RequiresPermissions("system:message:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyMessage yyMessage = yyMessageService.selectYyMessageById(id);
        mmap.put("yyMessage", yyMessage);
        //根据userId 获取 角色ID 判断 显示（管理用户要发的消息：）或 显示（家长恢复的消息：）
        SysRole sysRole = iSysRoleService.getRoleIdByUserId(getUserId());
        if (sysRole.getRoleId() == 3){
            mmap.put("roleId", "3");
        }else if (sysRole.getRoleId() == 4){
            mmap.put("roleId", "4");
        }
        mmap.put("jzUser", iSysUserService.getUserAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存消息管理
     */
    @RequiresPermissions("system:message:edit")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyMessage yyMessage)
    {
        return toAjax(yyMessageService.updateYyMessage(yyMessage));
    }

    /**
     * 删除消息管理
     */
    @RequiresPermissions("system:message:remove")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yyMessageService.deleteYyMessageByIds(ids));
    }
}
